<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="gtitle" style="margin-bottom: 20px"><a-icon type="info-circle" style="color: #108ee9" theme="filled"/>主机名配置</div>

      <div class="rowtxt">
        <a-row class='rowmb'>
          <a-col :span="2">主机名:</a-col>
          <a-col :span="4" class="blue"><a-input placeholder="localhost.patchserver"  v-model="config.hostName" @blur="updateconfig('hostName',config.hostName)"></a-input></a-col>
        </a-row>
      </div>

      <div class="gtitle" style="margin-bottom: 20px;margin-top: 20px"><a-icon type="info-circle" style="color: #108ee9" theme="filled"/>网卡配置</div>
      <a-table :columns="columns" :data-source="data"  >

      </a-table>

      <div class="gtitle" style="margin-bottom: 20px;margin-top: 20px"><a-icon type="info-circle" style="color: #108ee9" theme="filled"/>DNS配置</div>

      <div class="rowtxt">
        <a-row class='rowmb'>
          <a-col :span="2">首选DNS :</a-col>
          <a-col :span="4" class="blue"><a-input placeholder="8.8.8.8" v-model="config.firstChoiceDns" @blur="updateconfig('firstChoiceDns',config.firstChoiceDns)"></a-input></a-col>
        </a-row>
      </div>
      <div class="rowtxt">
        <a-row class='rowmb'>
          <a-col :span="2">备选DNS :</a-col>
          <a-col :span="4" class="blue"><a-input placeholder="8.8.8.8" v-model="config.spareChoiseDns" @blur="updateconfig('spareChoiseDns',config.spareChoiseDns)"></a-input></a-col>
        </a-row>
      </div>

      <div class="gtitle" style="margin-bottom: 20px;margin-top: 20px"><a-icon type="info-circle" style="color: #108ee9" theme="filled"/>网络探测工具</div>
      <a-spin :spinning="spinning">
      <div class="rowtxt">
        <a-row class='rowmb'>
          <a-col :span="2">探测方式</a-col>
          <a-col :span="3">
            <a-select v-model="exploreMethod">
              <a-select-option value="0">ping</a-select-option>
              <a-select-option value="1">telnet</a-select-option>
              <a-select-option value="2">ssh</a-select-option>
              <a-select-option value="3">traceroute</a-select-option>
            </a-select>
          </a-col>
          <a-col :span="2">探测地址</a-col>
          <a-col :span="3"><a-input v-model="testip"></a-input></a-col>
          <a-col :span="2" :offset="1"><a-button type="primary" @click="tance()">提交检测</a-button></a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2">网络探测输出信息:</a-col>
          <a-col :span="2" :offset="6"><a-checkbox v-model="needscroll"></a-checkbox> 滚动显示:</a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="11">
            <div class="infolist" v-html="testresult"></div>
          </a-col>
        </a-row>
      </div>
      </a-spin>
    </a-card>
  </page-header-wrapper>
</template>

<script>
  import { getnetconfig,updatenetconfig,getNetWorkCardList,networkDetection,getNetworkDetection } from '@/api/net'
  import {} from '../../utils/constant'
  export default {
    name: 'PatchUpdateManage',
    data() {
      return {
        data:[],
        needscroll:false,
        columns:[
          {
            title:'#',
            key:'#',
            dataIndex: '#',
            width:50
          },
          {
            title:'网卡名称',
            key:'网卡名称',
            dataIndex: '网卡名称',
          },
          {
            title:'状态',
            key:'状态',
            dataIndex: '状态',
          },
          {
            title:'接口速率',
            key:'接口速率',
            dataIndex: '接口速率',
          },
          {
            title:'IP地址',
            key:'IP地址',
            dataIndex: 'IP地址',
          },
          {
            title:'掩码',
            key:'掩码',
            dataIndex: '掩码',
          },
          {
            title:'网关',
            key:'网关',
            dataIndex: '网关',
          },
          {
            title:'操作',
            key:'操作',
            dataIndex: '操作',
          },
        ],
        exploreMethod:'0',
        config:{},
        testip:'',
        spinning:false,
        testresult:'',
        websocket: { }
      }
    },
    mounted() {
      this.$info(
        {
          title:'提示',
          content:'测试Lisence未授权！',
          onOk:()=> {
            this.$router.push({ path: '/overview/overview' })
          },
        }
      )
      this.getnetconfig()
      this.getNetWorklist()
      this.initwebsocket()
    },
    destroyed() {
      this.websocket.close()
    },
    methods:{
      tance(){
        this.testresult = ''
        this.websocket.send(JSON.stringify({
          probeType:this.exploreMethod,
          probeUrl:this.testip
        }))
        this.spinning = true
        // this.spinning = true
        // networkDetection({
        //   probeType:this.exploreMethod,
        //   probeUrl:this.testip
        // }).then(res=>{
        //   this.testresult = res.data
        //   this.spinning = false
        // })
      },
      getNetWorklist(){
        getNetWorkCardList({}).then(res=>{})
      },
      getnetconfig(){
        getnetconfig({id:1}).then(res=>{
          this.config = res.data
        })
      },
      updateconfig(key,value){
        let json = {}
        json.id = 1
        json[key] = value
        updatenetconfig(json).then(res=>{})
      },

      initwebsocket(){
        this.websocket = new WebSocket(getNetworkDetection());
          // websocket = new WebSocket("wss://"+window.location.host+"/socket/connect");
        this.websocket.onopen = this.onOpen.bind(this);
        // this.websocket.onclose = (evt)=> { this.onClose(evt) };
        this.websocket.onmessage = this.onMessage.bind(this);
        this.websocket.onerror = function(evt) { console.log(evt) };
      },
      onOpen(evt){
        console.log('success')
      },
      onMessage(evt){
        this.spinning = false
        this.testresult += `<div>${evt.data}</div>`
        if(this.needscroll){
          setTimeout(()=>{
            document.getElementsByClassName('infolist')[0].scrollTop = document.getElementsByClassName('infolist')[0].scrollHeight
          },100)
        }

      },
      onClose(){

      },
      onError(evt){
        console.log(evt)
      }
    },

  }
</script>

<style lang="less"  scoped>

  .gtitle{
    background-color: #e6f3fc;
    line-height: 40px;
    padding-left: 20px;
    color: #333;
    font-weight: bold;

    i{
      margin-right: 10px;
    }
  }
  .rowtxt,.ant-radio-group{
    line-height: 32px;

  }
  .ant-radio-group{
    width: 100%;
  }
  .rowspan{
    display: inline-block;
    width: 150px;
  }
  .showtxt{
    border: 1px solid #ddd;
    border-radius: 4px;
    padding: 0 10px;
    height: 120px;
    overflow-y: auto;
  }
  .rowmb{
    margin-bottom: 10px;
  }
  .blue{
    color: #3366FF;
  }
  .iplist{
    border: 1px solid #ddd;
    padding: 5px;
    min-height: 100px;
    line-height: 30px;
    div:hover{
      background-color: #efefef;
    }
    .icon{
      color: red;
      cursor: pointer;
      line-height: 30px;
      float: right;
      margin-right: 20px;
    }
  }
  .infolist{
    border: 1px solid #ddd;
    height: 200px;
    padding:10px;
    overflow-y: auto;
  }
</style>
